import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import axios from 'axios'
import * as step from '@flow-step/step-toolkit'

export const SEP = path.sep
export const EOL = os.EOL

export function emptyDir(path: string) {
    const files = fs.readdirSync(path)
    files.forEach(file => {
        const filePath = `${path}/${file}`
        const stats = fs.statSync(filePath)
        if (stats.isDirectory()) {
            emptyDir(filePath)
        } else {
            fs.unlinkSync(filePath)
        }
    })
}

export function rmEmptyDir(path: string, level = 0) {
    const files = fs.readdirSync(path)
    if (files.length > 0) {
        let tempFile = 0
        files.forEach(file => {
            tempFile++
            rmEmptyDir(`${path}/${file}`, 1)
        })
        if (tempFile === files.length && level !== 0) {
            fs.rmdirSync(path)
        }
    } else {
        level !== 0 && fs.rmdirSync(path)
    }
}

/**
 * @param {*} path
 */
export function clearDir(path: string) {
    emptyDir(path)
    rmEmptyDir(path)
}
